package cn.wolfcode.service.impl;

import cn.wolfcode.ann.RequiredPermission;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }
//此处需要修改   改成后台分页插件 PageInfo
    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<Permission>(permissions);
    }
    //项目启动的时候处理器映射器会收集所有路径和对应的方法
    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

@Autowired
private ApplicationContext ctx;
    @Override
    public void reload() {
        //获取数据库中所有权限表达式
       List<String> permissions = permissionMapper.selectAllExpression();

      /*  Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        Collection<HandlerMethod> values1 = handlerMethods.values();
        for (HandlerMethod handlerMethod : values1) {
            RequiredPermission annotation = handlerMethod.getMethodAnnotation(RequiredPermission.class);

            if (annotation == null) {
                continue;
            }
            //从注解中获取权限名字和表达式
            String name = annotation.name();
            String expression = annotation.expression();
//判断是否包含表达式
            if (permissions.contains(expression)) {
                Permission permission = new Permission();
                permission.setName(name);
                permission.setExpression(expression);
                //把权限传入
                permissionMapper.insert(permission);
                //插入完了一条数据 就放到集合 避免重复添加同一个name
                permissions.add(expression);
            }
        }
    }
}*/


        //从spring 容器中获取所有贴着Controller 注解的对象
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //从MAP中获取所有的value
        Collection<Object> values = map.values();
        for(Object bean:values){
            //遍历 获取字节码 反射
            Class<?> aClass = bean.getClass();
            //通过反射获取该字节码中的每个方法
            Method[] declaredMethods = aClass.getDeclaredMethods();
            for (Method declaredMethod : declaredMethods) {
                RequiredPermission annotation = declaredMethod.getAnnotation(RequiredPermission.class);
                //判断是否有注解
                if (annotation==null){
                    continue;
                }
                //从注解中获取权限名字和表达式
                String name = annotation.name();
                String expression = annotation.expression();
                //判断是否包含表达式
                if (!permissions.contains(expression)){
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    //把权限传入
                    permissionMapper.insert(permission);
                    //插入完了一条数据 就放到集合 避免重复添加同一个name
                    permissions.add(expression);
                }
            }
        }

    }
}

